var pomelo = require('pomelo');
var utils = require('../util/utils');
var userDao = module.exports;
//创建用户
userDao.createUser = function(nickName,loginTime ,cb){
    var  score = 0;
    var  coinNum = 0 ;
    var  diamondNum = 0 ;
    var  loginTimes = 1 ;
    var  winNum = 0 ;
    var sql = 'insert into user(nickName,score,coinNum,diamondNum,loginTime,loginTimes,winNum) values (?,?,?,?,?,?,?)';
    var args = [nickName,score,coinNum,diamondNum,loginTime,loginTimes,winNum];
    var dbclient = pomelo.app.get('dbclient');
    dbclient.insert(sql,args ,function(err,res){
        if(err){
            console.log('create user for userDao faild!');
            console.log(err);
            //utils.invokeCallback(cb, null,
        }else{
            console.log('create user ok!');
           // cb(null,res);
            utils.invokeCallback(cb, null,res);
        }

    });

};
//查询用户信息
userDao.getUserInfo = function(uid ,cb){
    var sql = 'select * from user where uid = ?';
    var args=[uid];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('userDao getUserInfo fail!!!!!!');
            console.log(err);
        }else{
            if(!!res && res.length===1){
                console.log('userDao getUserInfo start=======>');
                var rs = res[0];
               //  cb(null,rs);
                utils.invokeCallback(cb, null,rs);
            }else{
                console.log('userDao getUserInfo2 error ======>');
            }
        }
    });
};

//查询积分榜
userDao.getScoreList = function(target,cb){
    var sql = 'select uid,score,nickName from user order by score desc limit 0,?';
    var args=[target];
    console.log('args=',args);
    var dbclient = pomelo.app.get('dbclient');
    console.log('dbclinet:',dbclient);
    dbclient.query(sql,args,function(err,res){
        console.log('fasdddddddddddddddddddddddddd');
        if(err){
            console.log('userDao getScoreList fail');
            console.log(err);
        }else{
             if(!!res &&res.length===1){
                 console.log('userDao getScoreList start======>');
                 var rs = res[0];
                 console.log('rs===========>',rs);
                 utils.invokeCallback(cb,null,rs);
             }else{
                 //积分榜
                 console.log('userDao getScoreList=',res);
                 utils.invokeCallback(cb,null,res);
                }
             }
    });
};

//查询富豪榜
userDao.getRichList = function(target,cb){
    var sql = 'select uid,coinNum,nickName from user order by coinNum desc limit 0,?';
    var args=[target];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('userDao getRichList fail');
            console.log(err);
        }else{
            //返回富豪榜
            console.log('userDao getCoinList=',res);
            utils.invokeCallback(cb,null,res);
        }
    });
};
//更改昵称
userDao.updateNickName = function(nickName,uid,cb){
    var sql = 'update user set nickName = ? where uid =?';
    var args=[nikcName,uid];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
       if(err){
          console.log('userDao updateNickName fail');
          console.log(err);
       }else{
           if(!!res && res.affectedRows >0){
               utils.invokeCallback(cb ,null,true);
           }else{
               console.log('userDao updateNickName fail');
               utils.invokeCallback(cb,null,false);
           }
       }
    });
};
//根据昵称在数据库是否存在
userDao.getUserByNickName = function(nickName,cb){
    var sql = 'select uid from user where nickName =?';
    var args=[nickName];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('userDao getUserByNickName fail');
            console.log(err);
        }else{
            if(!!res &&res.length===1){
                utils.invokeCallback(cb,null,true);
            }else{
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};
//角色信息的管理
userDao.getActorInfo = function(uid,cb){
    var sql = 'select * from user_actor where uid =?';
    var args=[uid];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,actorList){
        if(err){
            console.log('userDao getActorInfo fail');
            console.log(err);
        }else{
            var actorArry =[];
            for(var i in actorList){
                var pieceData = [{ level:actorList[i].pLevel1},{ level:actorList[i].pLevel2},{ level:actorList[i].pLevel3},{ level:actorList[i].pLevel4}];
                var skillData = [{level:actorList[i].sLevel1},{level:actorList[i].sLevel2},{level:actorList[i].sLevel3}];
                var attrData =  [{level:actorList[i].aLevel1},{level:actorList[i].aLevel2},{level:actorList[i].aLevel3}];
                var actorJson ={pieces : JSON.parse(JSON.stringify(pieceData)) ,skills:JSON.parse(JSON.stringify(skillData)) ,attrs:JSON.parse(JSON.stringify(attrData)),active:actorList[i].active};
                actorArry.push(actorJson);
            }
            utils.invokeCallback(cb, null,JSON.parse(JSON.stringify(actorArry)));
        }
    });

};

//创建角色(角色管理插入)
userDao.createActor = function(uid,actorId,activity,cb){
    var sql = 'insert into user_actor values (?,?,1,1,1,1,1,1,1,1,1,1,?)';
    var args = [uid,actorId,activity];
    var dbclient = pomelo.app.get('dbclient');
    dbclient.insert(sql,args ,function(err,res){
        if(err){
            console.log('creatActor for userDao faild!');
            console.log(err);
        }else{
            console.log('createActor ok!');
            cb();
        }

    });
};
//通过uid、actorId获取当前用户的角色信息信息
userDao.getUserActorInfo= function(uid ,actorId,cb){
    var sql ='select a.* ,b.* from user a join user_actor b on a.uid = b.uid where a.uid=? and b.actorId =?';
    var args = [uid,actorId];
    console.log('args===============>',args);
    var dbclient = pomelo.app.get('dbclient');
    dbclient.query(sql,args,function(err ,res){
        if(err){
            console.log('creatActor for userDao faild!');
            console.log(err);
        }else{
            if(!!res && res.length===1){
                console.log('userDao getUserInfo start=======>');
                var rs = res[0];
                utils.invokeCallback(cb, null,rs);
            }else{
                console.log('userDao getUserInfo faild22222222');
            }
        }
    });
};

//更改用户信息(用户的胜场数)
userDao.updateUser = function(args,cb){
    var sql = 'update user set score=?,winNum  = ? where uid =?';
    var info =[args.score,args.winNum,args.uid];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,info,function(err,res){
        if(err){
            console.log('userDao updateUser fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                utils.invokeCallback(cb ,null,true);
            }else{
                console.log('userDao updateUser fail');
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};
//更改用户coinNum、diamondNum
userDao.updateUserInfo = function(args,cb){
    var sql = 'update user set coinNum  = ? ,diamondNum = ? where uid =?';
    var info =[args.coinNum,args.diamondNum,args.uid];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,info,function(err,res){
        if(err){
            console.log('userDao updateUserInfo fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                utils.invokeCallback(cb ,null,true);
            }else{
                console.log('userDao updateUserInfo fail');
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};

//更改用户信息
userDao.updateUserInfo2 = function(args,cb){
    var sql = 'update user set loginTime  = ? ,loginTimes = ? where uid =?';
    var info =[args.loginTime,args.loginTimes,args.uid];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,info,function(err,res){
        if(err){
            console.log('userDao updateUserInfo fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                utils.invokeCallback(cb ,null,true);
            }else{
                console.log('userDao updateUserInfo fail');
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};

//查询表中user_actor中的角色激活状况
userDao.getActorActive = function(uid,cb){
    var sql = 'select count(*) as total from where uid= ? and active=1';
    var args = [uid];
    var dbclient  = pomelo.app.get('dbclient');
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('userdao getActorActive fail');
            console.log('err=============>',err);
        }else{
            if(!!res && res.length===1){
                console.log('userDao getActorActive start=======>');
                var rs = res[0];
                utils.invokeCallback(cb, null,rs);
            }else{
                console.log('userDao getActorActive error ======>');
            }
        }
    });
};

//激活用户
userDao.updateUserActor = function(user,cb){
    var sql = 'update user_actor set active  = 1 where uid = ? and actorId = ?';
    var args =[user.loginTime,args.loginTimes,user.uid];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('userDao updateUserInfo fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
               console.log('userDao updateUserActor ok!!!!!');
                cb();
            }else{
                console.log('userDao updateUserActor fail');
                cb();
            }
        }
    });
};



